<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="id" label="编号">
				<uni-easyinput type="number" v-model="formData.id" />
			</uni-forms-item>
			<uni-forms-item name="body" label="内容">
				<uni-easyinput v-model="formData.body" />
			</uni-forms-item>
			<uni-forms-item name="cancelApply" label="取消订单请求">
				<uni-data-checkbox :multiple="true" v-model="formData.cancelApply" />
			</uni-forms-item>
			<uni-forms-item name="cancelStyle" label="取消类型">
				<uni-data-checkbox v-model="formData.cancelStyle" :localdata="formOptions.cancelStyle_localdata" />
			</uni-forms-item>
			<uni-forms-item name="cartCount" label="购物车数量">
				<uni-easyinput type="number" v-model="formData.cartCount" />
			</uni-forms-item>
			<uni-forms-item name="deliveryType" label="配送方式">
				<uni-data-checkbox v-model="formData.deliveryType" :localdata="formOptions.deliveryType_localdata" />
			</uni-forms-item>
			<uni-forms-item name="freight" label="运费">
				<uni-easyinput type="number" v-model="formData.freight" />
			</uni-forms-item>
			<uni-forms-item name="goods" label="商品">
				<uni-data-checkbox :multiple="true" v-model="formData.goods" />
			</uni-forms-item>
			<uni-forms-item name="isDelete" label="是否删除">
				<uni-data-checkbox v-model="formData.isDelete" :localdata="formOptions.isDelete_localdata" />
			</uni-forms-item>
			<uni-forms-item name="juli" label="距离">
				<uni-easyinput type="number" v-model="formData.juli" />
			</uni-forms-item>
			<uni-forms-item name="lastPayTime" label="最后支付时间">
				<uni-datetime-picker return-type="timestamp" :value="formData.lastPayTime" />
			</uni-forms-item>
			<uni-forms-item name="platform" label="操作平台">
				<uni-easyinput v-model="formData.platform" />
			</uni-forms-item>
			<uni-forms-item name="remark" label="备注">
				<uni-easyinput v-model="formData.remark" />
			</uni-forms-item>
			<uni-forms-item name="shopid" label="店铺">
				<uni-data-checkbox v-model="formData.shopid" collection="cloud_shops" field="name as text, id as value" />
			</uni-forms-item>
			<uni-forms-item name="state" label="状态">
				<uni-easyinput type="number" v-model="formData.state" />
			</uni-forms-item>
			<uni-forms-item name="totalDiscount" label="优惠金额">
				<uni-easyinput type="number" v-model="formData.totalDiscount" />
			</uni-forms-item>
			<uni-forms-item name="totalMoney" label="总金额">
				<uni-easyinput type="number" v-model="formData.totalMoney" />
			</uni-forms-item>
			<uni-forms-item name="uid" label="用户">
				<uni-data-checkbox v-model="formData.uid" collection="uni-id-users" field="_id as value, nickname as text" />
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/cloud_orders.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'cloud_orders';

	function getValidator(fields) {
		let reuslt = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				reuslt[key] = validator[key]
			}
		}
		return reuslt
	}

	export default {
		data() {
			return {
				formData: {
					"id": null,
					"address": null,
					"body": "",
					"cancelApply": [],
					"cancelStyle": "",
					"cartCount": null,
					"deliveryHour": null,
					"deliveryType": "",
					"freight": null,
					"goods": [],
					"isDelete": 0,
					"juli": null,
					"lastPayTime": null,
					"platform": "",
					"remark": "",
					"shopid": null,
					"state": null,
					"totalDiscount": null,
					"totalMoney": null,
					"uid": ""
				},
				formOptions: {
					"cancelStyle_localdata": [{
							"text": "自动",
							"value": "auto"
						},
						{
							"text": "用户",
							"value": "user"
						},
						{
							"text": "商家",
							"value": "shop"
						}
					],
					"deliveryType_localdata": [{
							"text": "送到家",
							"value": "deliveryHome"
						},
						{
							"text": "自提",
							"value": "selfRaising"
						}
					],
					"isDelete_localdata": [{
							"text": "否",
							"value": 0
						},
						{
							"text": "是",
							"value": 1
						}
					]
				},
				rules: {
					...getValidator(["id", "address", "body", "cancelApply", "cancelStyle", "cartCount", "deliveryHour",
						"deliveryType", "freight", "goods", "isDelete", "juli", "lastPayTime", "platform", "remark", "shopid", "state",
						"totalDiscount", "totalMoney", "uid"
					])
				}
			}
		},
		onLoad(e) {
			const id = e.id
			this.formDataId = id
			this.getDetail(id)
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.submit().then((res) => {
					this.submitForm(res)
				}).catch((errors) => {
					uni.hideLoading()
				})
			},

			submitForm(value) {
				// 使用 clientDB 提交数据
				db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					'id,address,body,cancelApply,cancelStyle,cartCount,deliveryHour,deliveryType,freight,goods,isDelete,juli,lastPayTime,platform,remark,shopid,state,totalDiscount,totalMoney,uid'
				).get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
